package com.ivi.car.module.home.view.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ivi.car.R;
import com.ivi.car.base.BaseActivity;
import com.ivi.car.bean.home.CarMaintainInfoBean;
import com.ivi.car.bean.home.CarStatusInfoBean;
import com.ivi.car.bean.home.HomeAdvBean;
import com.ivi.car.module.carstate.view.activity.CarStateActivity;
import com.ivi.car.module.home.model.HomeModel;
import com.ivi.car.module.home.model.IHomeContract;
import com.ivi.car.module.home.presenter.HomePresenter;
import com.ivi.car.module.home.view.adapter.CarMaintainAdapter;
import com.ivi.car.module.home.view.adapter.CarStatusInfoAdapter;
import com.ivi.car.module.home.view.adapter.HomeAdvAdapter;
import com.ivi.car.module.maintain.view.MaintainCarActivity;
import com.ivi.car.module.reservation.activity.ReservationActivity;
import com.wm.machine.baselibrary.common.widget.recyclerview.itemdecoration.SpaceItemDecoration;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Copyright
 * author zhengcheng
 * date 2018/3/6
 * describe 首页
 * history
 */

public class HomeActivity extends BaseActivity implements IHomeContract.View {
  private static final int STATUS_SPACE = 5;
  private static final int MAINTAIN_SPACE = 8;
  private static final int SPAN_COUNT = 2;
  @BindView(R.id.home_head_title_rl)
  RelativeLayout homeHeadTitleRl;
  @BindView(R.id.home_car_status_img)
  ImageView homeCarStatusImg;
  @BindView(R.id.home_car_status_tv)
  TextView homeCarStatusTv;
  @BindView(R.id.home_car_status_rv)
  RecyclerView homeCarStatusRv;
  @BindView(R.id.arrow_right_icon)
  ImageView arrowRightIcon;
  @BindView(R.id.home_head_rl)
  RelativeLayout homeHeadRl;
  @BindView(R.id.home_order_maintain_tv)
  TextView homeOrderMaintainTv;
  @BindView(R.id.home_maintain_type_rv)
  RecyclerView homeMaintainTypeRv;
  @BindView(R.id.home_advertise_rv)
  RecyclerView homeAdvertiseRv;
  private HomePresenter homePresenter;

  @Override
  protected int getLayoutId() {
    return R.layout.home_activity;
  }

  @Override
  protected void initView() {

  }

  @Override
  protected void initData() {
    homePresenter = new HomePresenter(this, mLifecycleProvider);
    homePresenter.loadCarStatusInfo();
    homePresenter.loadMaintainInfo();
    homePresenter.loadHomeAdv();
  }

  @Override
  public void showCarStatusInfo(CarStatusInfoBean carStatusInfo) {
    switch (carStatusInfo.getStatus()) {
      case HomeModel.STATUS_NORMAL:
        homeHeadRl.setBackgroundColor(Color.parseColor("#1A844C"));
        homeCarStatusImg.setImageResource(R.mipmap.home_car_normal_ic);
        homeOrderMaintainTv.setText("您的爱车保养的很好");
        homeOrderMaintainTv.setBackgroundColor(Color.parseColor("#219D5F"));
        break;
      case HomeModel.STATUS_MAINTAIN:
        homeHeadRl.setBackgroundColor(Color.parseColor("#DE6C1B"));
        homeCarStatusImg.setImageResource(R.mipmap.home_car_maintain_ic);
        homeOrderMaintainTv.setText("立即预约保养");
        homeOrderMaintainTv.setBackgroundColor(Color.parseColor("#F1862E"));
        break;
      case HomeModel.STATUS_FAULT:
        homeHeadRl.setBackgroundColor(Color.parseColor("#D03641"));
        homeCarStatusImg.setImageResource(R.mipmap.home_car_fault_ic);
        homeOrderMaintainTv.setText("立即预约维修");
        homeOrderMaintainTv.setBackgroundColor(Color.parseColor("#DA4A4D"));
        break;
      default:
        break;
    }
    homeCarStatusTv.setText(carStatusInfo.getStatusTitle());
    CarStatusInfoAdapter carStatusInfoAdapter =
        new CarStatusInfoAdapter(R.layout.item_home_car_status, carStatusInfo.getCarInfos());
    LinearLayoutManager layoutManager =
        new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    homeCarStatusRv.addItemDecoration(new SpaceItemDecoration(0, STATUS_SPACE));
    homeCarStatusRv.setLayoutManager(layoutManager);
    homeCarStatusRv.setAdapter(carStatusInfoAdapter);
    carStatusInfoAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
      @Override
      public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        startActivity(new Intent(HomeActivity.this, CarStateActivity.class));
      }
    });
  }

  @Override
  public void showMaintainInfo(List<CarMaintainInfoBean> list) {
    CarMaintainAdapter maintainAdapter = new CarMaintainAdapter(R.layout.item_home_maintain, list);
    GridLayoutManager layoutManager =
        new GridLayoutManager(this, SPAN_COUNT, GridLayoutManager.VERTICAL, false);
    homeMaintainTypeRv.addItemDecoration(new SpaceItemDecoration(MAINTAIN_SPACE, MAINTAIN_SPACE));
    homeMaintainTypeRv.setLayoutManager(layoutManager);
    homeMaintainTypeRv.setAdapter(maintainAdapter);
    maintainAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
      @Override
      public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        startActivity(new Intent(HomeActivity.this, ReservationActivity.class));
      }
    });
  }

  @Override
  public void showHomeAdv(List<HomeAdvBean> list) {
    HomeAdvAdapter homeAdvAdapter = new HomeAdvAdapter(R.layout.item_home_adv, list);
    LinearLayoutManager layoutManager =
        new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    homeAdvertiseRv.addItemDecoration(new SpaceItemDecoration(0, STATUS_SPACE));
    homeAdvertiseRv.setLayoutManager(layoutManager);
    homeAdvertiseRv.setAdapter(homeAdvAdapter);
  }

  /**
   * 点击事件处理
   * 
   * @param view 点击事件对应的view
   */
  @OnClick({R.id.home_car_status_rl, R.id.maintainCar})
  public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.maintainCar:
        // 调到养车模块
        startActivity(new Intent(HomeActivity.this, MaintainCarActivity.class));
        break;
      default:
        break;
    }
  }

}
